﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace PictureOrganizer2.DataContainer
{
    public class GroupByContainer
    {
        public event EventHandler OnChanged;
        public delegate void GroupByChangedDelegate(GroupByContainer groupByContainer);
        private AppSettingsContainer config = new AppSettingsContainer();

        public GroupByContainer()
        {
            switch (config.GroupBy)
            {
                case "Language":
                    groupByLanguage = true;
                    break;
                case "Author":
                    groupByAuthor = true;
                    break;
                case "Folder":
                    groupByFolder = true;
                    break;
                default:
                    groupByNone = true;
                    break;
            }
        }

        private bool groupByLanguage = false;
        public bool GroupByLanguage
        {
            get { return groupByLanguage; }
            set
            {
                ResetGroupBy();
                bool diff = (this.groupByLanguage != value);
                groupByLanguage = value;
                SaveGroupBy("Language");
                if (diff)
                    RaiseOnChanged();
            }
        }

        private bool groupByAuthor = false;
        public bool GroupByAuthor
        {
            get { return groupByAuthor; }
            set
            {
                ResetGroupBy();
                bool diff = (this.groupByAuthor != value);
                groupByAuthor = value;
                SaveGroupBy("Author");
                if (diff)
                    RaiseOnChanged();
            }
        }

        private bool groupByFolder = false;
        public bool GroupByFolder
        {
            get { return groupByFolder; }
            set
            {
                ResetGroupBy();
                bool diff = (this.groupByFolder != value);
                groupByFolder = value;
                SaveGroupBy("Folder");
                if (diff)
                    RaiseOnChanged();
            }
        }

        private bool groupByNone = false;
        public bool GroupByNone
        {
            get { return groupByNone; }
            set
            {
                ResetGroupBy();
                bool diff = (this.groupByNone != value);
                groupByNone = value;
                SaveGroupBy("None");
                if (diff)
                    RaiseOnChanged();
            }
        }

        private void SaveGroupBy(string groupBy)
        {
            config.GroupBy = groupBy;
            config.SaveConfig();
        }

        private void ResetGroupBy()
        {
            groupByAuthor = false;
            groupByLanguage = false;
            groupByFolder = false;
            groupByNone = false;
        }

        private void RaiseOnChanged()
        {
            if(this.OnChanged != null)
                OnChanged(this, EventArgs.Empty);
        }

        public void SetGroupKeyGetter()
        {
            RaiseOnChanged();
        }
    }
}
